home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / LIGHTP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.0 KB  |  316 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "texture.h"
  5. #include <GL/glut.h>
  6.  
  7. #ifndef __sgi
  8. /* Most math.h's do not define float versions of the math functions. */
  9. #define expf(x) ((float)exp((x)))
  10. #define fabsf(x) ((float)fabs((x)))
  11. #endif
  12.  
  13. static int pstyle;
  14. static float transx, transy, rotx, roty;
  15. static int ox = -1, oy = -1;
  16. static int mot;
  17. #define PAN    1
  18. #define ROT    2
  19.  
  20. void
  21. pan(int x, int y) {
  22.     transx +=  (x-ox)/500.;
  23.     transy -= (y-oy)/500.;
  24.     ox = x; oy = y;
  25.     glutPostRedisplay();
  26. }
  27.  
  28. void
  29. rotate(int x, int y) {
  30.     rotx += x-ox;
  31.     if (rotx > 360.) rotx -= 360.;
  32.     else if (rotx < -360.) rotx += 360.;
  33.     roty += y-oy;
  34.     if (roty > 360.) roty -= 360.;
  35.     else if (roty < -360.) roty += 360.;
  36.     ox = x; oy = y;
  37.     glutPostRedisplay();
  38. }
  39.  
  40. void
  41. motion(int x, int y) {
  42.     if (mot == PAN) pan(x, y);
  43.     else if (mot == ROT) rotate(x,y);
  44. }
  45.  
  46. void
  47. mouse(int button, int state, int x, int y) {
  48.     if(state == GLUT_DOWN) {
  49.     switch(button) {
  50.     case GLUT_LEFT_BUTTON:
  51.         mot = PAN;
  52.         motion(ox = x, oy = y);
  53.         break;
  54.     case GLUT_RIGHT_BUTTON:
  55.         mot = ROT;
  56.         motion(ox = x, oy = y);
  57.         break;
  58.     case GLUT_MIDDLE_BUTTON:
  59.         break;
  60.     }
  61.     } else if (state == GLUT_UP) {
  62.     mot = 0;
  63.     }
  64. }
  65.  
  66. void pfunc(void) { pstyle = (pstyle+1) % 4; }
  67.  
  68. void help(void) {
  69.     printf("Usage: lightp [image]\n");
  70.     printf("'h'            - help\n");
  71.     printf("'p'            - toggle point mode\n");
  72.     printf("left mouse     - pan\n");
  73.     printf("right mouse    - rotate\n");
  74. }
  75.  
  76. void init(char *filename) {
  77.     GLfloat fog_color[4], fog_density = 0.05, density, far_cull;
  78.     unsigned *image;
  79.     int width, height, components;
  80.     if (filename) {
  81.     image = read_texture(filename, &width, &height, &components);
  82.     if (image == NULL) {
  83.         fprintf(stderr, "Error: Can't load image file \"%s\".\n",
  84.             filename);
  85.         exit(EXIT_FAILURE);
  86.     } else {
  87.         printf("%d x %d image loaded\n", width, height);
  88.     }
  89.     if (components != 1 && components != 2) {
  90.         printf("must be a l or la image\n");
  91.         exit(EXIT_FAILURE);
  92.     }
  93.     if (components == 1) {
  94.         /* hack for RE */
  95.         int i;
  96.         GLubyte *p = (GLubyte *)image;
  97.         for(i = 0; i < width*height; i++) {
  98.         p[i*4+3] = p[i*4+0];
  99.         }
  100.         components = 2;
  101.     }
  102.     } else {
  103.     int i, j;
  104.     unsigned char *img;
  105.     components = 4; width = height = 512;
  106.     image = (unsigned *) malloc(width*height*sizeof(unsigned));
  107.     img = (unsigned char *)image;
  108.     for (j = 0; j < height; j++)
  109.         for (i = 0; i < width; i++) {
  110.         int w2 = width/2, h2 = height/2;
  111.         if (i & 32)
  112.             img[4*(i+j*width)+0] = 0xff;
  113.         else
  114.             img[4*(i+j*width)+1] = 0xff;
  115.         if (j&32)
  116.             img[4*(i+j*width)+2] = 0xff;
  117.         if ((i-w2)*(i-w2) + (j-h2)*(j-h2) > 64*64 &&
  118.             (i-w2)*(i-w2) + (j-h2)*(j-h2) < 300*300) img[4*(i+j*width)+3] = 0xff;
  119.         }
  120.  
  121.     }
  122.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  123.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  124.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  125.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  126.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  127.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  128.     glTexImage2D(GL_TEXTURE_2D, 0, components, width,
  129.                  height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  130.                  image);
  131.     /*glEnable(GL_TEXTURE_2D);*/
  132.     glMatrixMode(GL_PROJECTION);
  133.     glLoadIdentity();
  134.     gluPerspective(50.,1.,.1,far_cull = 10.);
  135.     glMatrixMode(GL_MODELVIEW);
  136.     glLoadIdentity();
  137.     glTranslatef(0.,0.,-5.5);
  138.  
  139.     density = 1.- expf(-5.5 * fog_density * fog_density *
  140.                   far_cull * far_cull);
  141.  
  142. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  143. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  144.     density = MAX(MIN(density, 1.), 0.);
  145.  
  146.     fog_color[0] = .23*.19 + density *.57*.19;
  147.     fog_color[1] = .35*.19 + density *.45*.19;
  148.     fog_color[2] = .78*.19 + density *.22*.19;
  149.  
  150.     glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.f);
  151.  
  152.     glFogi(GL_FOG_MODE, GL_EXP2);
  153.     glFogf(GL_FOG_DENSITY, fog_density);
  154.     glFogfv(GL_FOG_COLOR, fog_color);
  155.     if (fog_density > 0)
  156.     glEnable(GL_FOG);
  157.     glLineWidth(2.0f);
  158.     glEnable(GL_LINE_SMOOTH);
  159.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  160.     glPointSize(10.f);
  161.     glEnable(GL_POINT_SMOOTH);
  162.     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  163. }
  164.  
  165. void draw_base(void) {
  166.     glColor4f(.1, .3, .1, 1.0);
  167.     glBegin(GL_QUADS);
  168.     glTexCoord2f(0, 0); glVertex3f(-1.f, 0.f, -1.f);
  169.     glTexCoord2f(0, 1); glVertex3f(-1.f, 0.f,  1.f);
  170.     glTexCoord2f(1, 1); glVertex3f( 1.f, 0.f,  1.f);
  171.     glTexCoord2f(1, 0); glVertex3f( 1.f, 0.f, -1.f);
  172.     glEnd();
  173. }
  174.  
  175. void draw_runway(void) {
  176.     glColor4f(.1, .1, .1, 1.0);
  177.     glPushMatrix();
  178.     glScalef(.1f, 1.f, 1.f);
  179.     glBegin(GL_QUADS);
  180.     glTexCoord2f(0, 0); glVertex3f(-1.f, 0.f, -1.f);
  181.     glTexCoord2f(0, 1); glVertex3f(-1.f, 0.f,  1.f);
  182.     glTexCoord2f(1, 1); glVertex3f( 1.f, 0.f,  1.f);
  183.     glTexCoord2f(1, 0); glVertex3f( 1.f, 0.f, -1.f);
  184.     glEnd();
  185.     glPopMatrix();
  186. }
  187.  
  188. /* v0 = v1*mat */
  189. void xform(float *v0, float *mat, float *v1) {
  190.     v0[0] = v1[0]*mat[0] + v1[1]*mat[4] + v1[2]*mat[8] + v1[3]*mat[12];
  191.     v0[1] = v1[1]*mat[1] + v1[1]*mat[5] + v1[2]*mat[9] + v1[3]*mat[13];
  192.     v0[2] = v1[2]*mat[2] + v1[1]*mat[6] + v1[2]*mat[10] + v1[3]*mat[14];
  193.     v0[3] = v1[3]*mat[3] + v1[1]*mat[7] + v1[2]*mat[11] + v1[3]*mat[15];
  194. }
  195.  
  196. /* m0 = m1*m2 */
  197. void xformm(float *m0, float *m1, float *m2) {
  198.     int i, j;
  199.     for(i = 0; i < 4; i++) {
  200.     for(j = 0; j < 4; j++) {
  201.         m0[4*i+j] = m1[4*i+0]*m2[4*0+j] + m1[4*i+1]*m2[4*1+j] + m1[4*i+2]*m2[4*2+j] + m1[4*i+3]*m2[4*3+j];
  202.     }
  203.     }
  204. }
  205.  
  206. void draw_quad(float x, float y, float z) {
  207.     glPushMatrix();
  208.     glTranslatef(x, y, z);
  209.     glRotatef(90.f, 1.f, 0.f, 0.f);
  210.     glScalef(.03f, .03f, .03f);
  211.     glBegin(GL_QUADS);
  212.     glTexCoord2f(0, 0); glVertex3f(-1.f, 0.f, -1.f);
  213.     glTexCoord2f(0, 1); glVertex3f(-1.f, 0.f,  1.f);
  214.     glTexCoord2f(1, 1); glVertex3f( 1.f, 0.f,  1.f);
  215.     glTexCoord2f(1, 0); glVertex3f( 1.f, 0.f, -1.f);
  216.     glEnd();
  217.     glPopMatrix();
  218. }
  219.  
  220. void draw_lights(void) {
  221.     int i;
  222.     GLfloat mat[16], matv[16], matp[16];
  223.     float v0[4], v1[4], v[4];
  224.  
  225.     glEnable(GL_BLEND);
  226.     glColor4f(1.f, 1.f, 1.f, 1.0);
  227.     glPushMatrix();
  228.     if (pstyle != 3) {
  229.     glScalef(.1f, 1.f, 1.f);
  230.     if (pstyle == 0) glDisable(GL_POINT_SMOOTH);
  231.     else glEnable(GL_POINT_SMOOTH);
  232.     if (pstyle == 1) glPointSize(13.f);
  233.     glGetFloatv(GL_MODELVIEW_MATRIX, matv);
  234.     glGetFloatv(GL_PROJECTION_MATRIX, matp);
  235.     xformm(mat, matv, matp);
  236.     v[0] = -1.f;
  237.     v[1] = 0.0f;
  238.     v[3] = 1.0f;
  239.     for(i = 0; i <= 20; i++) {
  240.         if (pstyle == 2) {
  241.         float s;
  242.         v[2] = -1.f+2.f/20*i;
  243.         v[0] -= 1.;
  244.         xform(v0, mat, v);
  245.         v[0] += 2.;
  246.         xform(v1, mat, v);
  247.         v[0] -= 1.;
  248.         s = fabsf(v0[0]/v0[3] - v1[0]/v1[3]);
  249.         glPointSize(10.f*s);
  250.         }
  251.         glBegin(GL_POINTS);
  252.         glVertex3f(-1.f, 0.f, -1.f+2.f/20*i);
  253.         glVertex3f( 1.f, 0.f, -1.f+2.f/20*i);
  254.         glEnd();
  255.     }
  256.     } else {
  257.     glEnable(GL_TEXTURE_2D);
  258.     glScalef(1.f, 10.f, 1.f);
  259.     for(i = 0; i <= 20; i++) {
  260.         float v = -1.f+2.f/20*i;
  261.         draw_quad(-.1f, 0.f, v);
  262.         draw_quad( .1f, 0.f, v);
  263.     }
  264.     glDisable(GL_TEXTURE_2D);
  265.     }
  266.     glPopMatrix();
  267.     glDisable(GL_BLEND);
  268. }
  269.  
  270. void display(void) {
  271.     glClear(GL_COLOR_BUFFER_BIT);
  272.  
  273.     glPushMatrix();
  274.     glTranslatef(transx, transy, 0.f);
  275.     glRotatef(rotx, 0., 1., 0.);
  276.     glRotatef(roty, 1., 0., 0.);
  277.     glScalef(10.f,1.f,10.f);
  278.     glTranslatef(0.f,-.4f,0.f);
  279.     draw_base();
  280.     draw_runway();
  281.     draw_lights();
  282.     glPopMatrix();
  283.     glutSwapBuffers();
  284. }
  285.  
  286. void reshape(int w, int h) {
  287.     glViewport(0, 0, w, h);
  288. }
  289.  
  290. /*ARGSUSED1*/
  291. void
  292. key(unsigned char key, int x, int y) {
  293.     switch(key) {
  294.     case 'p': pfunc(); break;
  295.     case 'h': help(); break;
  296.     case '\033': exit(EXIT_SUCCESS); break;
  297.     default: break;
  298.     }
  299.     glutPostRedisplay();
  300. }
  301.  
  302. int main(int argc, char** argv) {
  303.     glutInitWindowSize(512, 512);
  304.     glutInit(&argc, argv);
  305.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  306.     (void)glutCreateWindow(argv[0]);
  307.     init(argc == 1 ? "data/light.bw" : argv[1]);
  308.     glutDisplayFunc(display);
  309.     glutKeyboardFunc(key);
  310.     glutReshapeFunc(reshape);
  311.     glutMouseFunc(mouse);
  312.     glutMotionFunc(motion);
  313.     glutMainLoop();
  314.     return 0;
  315. }
  316.